%%% This code runs the comparative statics as in Figure D1

clear;close all;clc

%% comparative statistics
%% change residential_share 
beta=0.98;
residential_share=0.5674;
HC_HR=1;
HC=HC_HR*(1-residential_share);
HR=HC_HR*residential_share;
gamma = 0.5369;
phi = 0.2;
phiC = 0.2;
phiR = 0.2;
alpha=0.38; %% Prod Fn Para
A=1/HC^alpha;

kappa_var=linspace(0,1,100);
eqm =[]; %% baseline
eqm1=[]; %% High Capital Flow Shock

%% alternative calibration

HC_al=HC_HR*(1-.2);
HR_al=HC_HR*.2;
eqm2 =[]; %% baseline
eqm3=[]; %% High Capital Flow Shock

for i = 1: length(kappa_var)
    model_implied=solve_eqm_calibration(beta,phi,phiR,phiC,HC,HR,gamma,kappa_var(i),A);
    eqm = [eqm; model_implied];
    model_implied=solve_eqm_calibration(beta,2*phi,2*phiR,2*phiC,HC,HR,gamma,kappa_var(i),A);
    eqm1 = [eqm1; model_implied];
    
    %% altenative measure
    model_implied=solve_eqm_calibration(beta,phi,phiR,phiC,HC_al,HR_al,gamma,kappa_var(i),A);
    eqm2 = [eqm2; model_implied];
    model_implied=solve_eqm_calibration(beta,2*phi,2*phiR,2*phiC,HC_al,HR_al,gamma,kappa_var(i),A);
    eqm3 = [eqm3; model_implied];
end

figure(1)
plot(kappa_var,eqm(:,1),'color','blue','linewidth',4)
set(gcf, 'PaperPosition', [0 0 5 5]); %Position the plot further to the left and down. Extend the plot to fill entire paper.
set(gcf, 'PaperSize', [5 5]); %Keep the same paper size
set(gca,'FontSize', 15)
saveas(gcf,'hr1_new.pdf')

figure(2)
plot(kappa_var,eqm(:,2),'color','blue','linewidth',4)
set(gcf, 'PaperPosition', [0 0 5 5]); %Position the plot further to the left and down. Extend the plot to fill entire paper.
set(gcf, 'PaperSize', [5 5]); %Keep the same paper size
set(gca,'FontSize', 15)
saveas(gcf,'hc1_new.pdf')

figure(3)
plot(kappa_var,eqm(:,4),'color','blue','linewidth',4)
set(gcf, 'PaperPosition', [0 0 5 5]); %Position the plot further to the left and down. Extend the plot to fill entire paper.
set(gcf, 'PaperSize', [5 5]); %Keep the same paper size
set(gca,'FontSize', 15)
saveas(gcf,'c1_new.pdf')




figure(4)
plot(kappa_var,eqm(:,3),'color','blue','linewidth',4)
set(gcf, 'PaperPosition', [0 0 5 5]); %Position the plot further to the left and down. Extend the plot to fill entire paper.
set(gcf, 'PaperSize', [5 5]); %Keep the same paper size
set(gca,'FontSize', 15)
saveas(gcf,'y1_new.pdf')


figure(5)
plot(kappa_var,eqm(:,6),'color','blue','linewidth',4);hold on
plot(kappa_var,eqm1(:,6),':','color','blue','linewidth',4);
legend('baseline','high capital flow shock','FontSize',15,'Location','northeast')
set(gcf, 'PaperPosition', [0 0 5 5]); %Position the plot further to the left and down. Extend the plot to fill entire paper.
set(gcf, 'PaperSize', [5 5]); %Keep the same paper size
set(gca,'FontSize', 15)
saveas(gcf,'pr1_new.pdf')

figure(6)
plot(kappa_var,eqm(:,5),'color','blue','linewidth',4);hold on
plot(kappa_var,eqm1(:,5),':','color','blue','linewidth',4);hold on
legend('baseline','high capital flow shock','FontSize',15,'Location','northwest')
set(gcf, 'PaperPosition', [0 0 5 5]); %Position the plot further to the left and down. Extend the plot to fill entire paper.
set(gcf, 'PaperSize', [5 5]); %Keep the same paper size
set(gca,'FontSize', 15)
saveas(gcf,'pc1_new.pdf')


figure(7)
plot(kappa_var,eqm2(:,1),'color','blue','linewidth',4)
set(gcf, 'PaperPosition', [0 0 5 5]); %Position the plot further to the left and down. Extend the plot to fill entire paper.
set(gcf, 'PaperSize', [5 5]); %Keep the same paper size
set(gca,'FontSize', 15)
saveas(gcf,'hr2_new.pdf')

figure(8)
plot(kappa_var,eqm2(:,2),'color','blue','linewidth',4)
set(gcf, 'PaperPosition', [0 0 5 5]); %Position the plot further to the left and down. Extend the plot to fill entire paper.
set(gcf, 'PaperSize', [5 5]); %Keep the same paper size
set(gca,'FontSize', 15)
saveas(gcf,'hc2_new.pdf')

figure(9)
plot(kappa_var,eqm2(:,4),'color','blue','linewidth',4)
%title('$c$','interpret','latex')
set(gcf, 'PaperPosition', [0 0 5 5]); %Position the plot further to the left and down. Extend the plot to fill entire paper.
set(gcf, 'PaperSize', [5 5]); %Keep the same paper size
set(gca,'FontSize', 15)
saveas(gcf,'c2_new.pdf')

figure(10)
plot(kappa_var,eqm2(:,3),'color','blue','linewidth',4)
%title('$y$','interpret','latex')
set(gcf, 'PaperPosition', [0 0 5 5]); %Position the plot further to the left and down. Extend the plot to fill entire paper.
set(gcf, 'PaperSize', [5 5]); %Keep the same paper size
set(gca,'FontSize', 15)
saveas(gcf,'y2_new.pdf')




figure(11)
plot(kappa_var,eqm2(:,6),'color','red','linewidth',4);hold on
plot(kappa_var,eqm3(:,6),':','color','red','linewidth',4);
legend('baseline','high capital flow shock','FontSize',15,'Location','northwest')
%title('$p^R$','interpret','latex')
set(gcf, 'PaperPosition', [0 0 5 5]); %Position the plot further to the left and down. Extend the plot to fill entire paper.
set(gcf, 'PaperSize', [5 5]); %Keep the same paper size
set(gca,'FontSize', 15)
saveas(gcf,'pr2_new.pdf')

figure(12)
plot(kappa_var,eqm2(:,5),'color','blue','linewidth',4);hold on
plot(kappa_var,eqm3(:,5),':','color','blue','linewidth',4);hold on
legend('baseline','high capital flow shock','FontSize',15,'Location','northwest')
set(gcf, 'PaperPosition', [0 0 5 5]); %Position the plot further to the left and down. Extend the plot to fill entire paper.
set(gcf, 'PaperSize', [5 5]); %Keep the same paper size
set(gca,'FontSize', 15)
saveas(gcf,'pc2_new.pdf')

